//
//  SSOverlapScaleAndRangePanelController.m
//  Investigation Catalyst
//
//  Created by Tobias Haughton on Wed Jan 07 2004.
//  Copyright (c) 2004 Starline Software Ltd. All rights reserved.
//

#import "SSOverlapScaleAndRangePanelController.h"
#import "SSCatalystDocument.h"

@implementation SSOverlapScaleAndRangePanelController

+ (id)sharedOverlapScaleAndRangePanelController {
   static SSOverlapScaleAndRangePanelController *sharedOverlapScaleAndRangePanelController = nil;

   if(!sharedOverlapScaleAndRangePanelController) {
      sharedOverlapScaleAndRangePanelController = [[SSOverlapScaleAndRangePanelController allocWithZone:[self zone]] init];
   }

   return sharedOverlapScaleAndRangePanelController;
}

- (id)init {
   self = [super initWithWindowNibName:@"OverlapScaleAndRangePanel"];
   if (self) {
      [self setWindowFrameAutosaveName:@"OverlapScaleAndRangePanel"];
   }
   return self;
}

- (void)setMainWindow:(NSWindow *)mainWindow {
   if([(SSCatalystDocument *)[[NSDocumentController sharedDocumentController] currentDocument] displayMode] != SSOverlapDisplayMode) {
      [self close];
   }
   
   needsUpdate = YES;
}

- (void)setControlValues
{
   if(representedObject) {
      [presetScaleMatrix setEnabled:YES];
      [unitPopup setEnabled:YES];
      [pixelField setEnabled:YES];

      SSCatalystDocument *currentDocument = [[NSDocumentController sharedDocumentController] currentDocument];
      
      if([currentDocument overlapUnit] == SSMillisecondUnit) {
         [unitPopup selectItemAtIndex:SSMillisecondUnitKey];
      } else if([currentDocument overlapUnit] == SS100THSecondUnit) {
         [unitPopup selectItemAtIndex:SS100THSecondUnitKey];
      } else if([currentDocument overlapUnit] == SSSecondUnit) {
         [unitPopup selectItemAtIndex:SSSecondUnitKey];
      } else if([currentDocument overlapUnit] == SSMinuteUnit) {
         [unitPopup selectItemAtIndex:SSMinuteUnitKey];
      } else if([currentDocument overlapUnit] == SSHourUnit) {
         [unitPopup selectItemAtIndex:SSHourUnitKey];
      } else {
         [unitPopup selectItemAtIndex:SSDayUnitKey];
      }

      //set the pixels value
      [pixelField setFloatValue:[self convertPixelsPerUnitToUnitsPerInch:[currentDocument overlapPixelsPerUnit]]];

      [self setPresetMatrixSelection:self];
   } else {
      //there is no document so disable the controls
      [presetScaleMatrix setEnabled:NO];
      [unitPopup setEnabled:NO];
      [pixelField setEnabled:NO];
   }

   [super setControlValues];
}

- (IBAction)updateChartValues:(id)sender
{
   SSCatalystDocument *currentDocument = [[NSDocumentController sharedDocumentController] currentDocument];
   
   if([sender isEqualTo:unitPopup]) {
      switch([[unitPopup selectedItem] tag]) {
         case SSMillisecondUnitKey:[currentDocument setOverlapUnit:SSMillisecondUnit];
            break;
         case SS100THSecondUnitKey:[currentDocument setOverlapUnit:SS100THSecondUnit];
            break;
         case SSSecondUnitKey:[currentDocument setOverlapUnit:SSSecondUnit];
            break;
         case SSMinuteUnitKey:[currentDocument setOverlapUnit:SSMinuteUnit];
            break;
         case SSHourUnitKey:[currentDocument setOverlapUnit:SSHourUnit];
            break;
         default:[currentDocument setOverlapUnit:SSDayUnit];
            break;
      }

      [self setPresetMatrixSelection:self];
   } else if([sender isEqualTo:pixelField]) {
      [currentDocument setOverlapPixelsPerUnit:[self convertUnitsPerInchToPixelsPerUnit:[pixelField floatValue]]];
      [self setPresetMatrixSelection:self];
   } else if([sender isEqualTo:presetScaleMatrix]) {
      [self presetScaleMatrixSelectionChanged:self];
      
      switch([[unitPopup selectedItem] tag]) {
         case SSMillisecondUnitKey:[currentDocument setOverlapUnit:SSMillisecondUnit];
            break;
         case SS100THSecondUnitKey:[currentDocument setOverlapUnit:SS100THSecondUnit];
            break;
         case SSSecondUnitKey:[currentDocument setOverlapUnit:SSSecondUnit];
            break;
         case SSMinuteUnitKey:[currentDocument setOverlapUnit:SSMinuteUnit];
            break;
         case SSHourUnitKey:[currentDocument setOverlapUnit:SSHourUnit];
            break;
         default:[currentDocument setOverlapUnit:SSDayUnit];
            break;
      }

      [currentDocument setOverlapPixelsPerUnit:[self convertUnitsPerInchToPixelsPerUnit:[pixelField floatValue]]];
   }
}


- (IBAction)setPresetMatrixSelection:(id)sender;
{
   //select a preset matrix button if the current unit and pixels match
   if([[unitPopup selectedItem] tag] == SSMillisecondUnitKey && [self convertUnitsPerInchToPixelsPerUnit:[pixelField floatValue]] == 20) {
      //milisecond preset
      [presetScaleMatrix selectCellWithTag:SSOverlapPresetMillisecondKey];
   } else if([[unitPopup selectedItem] tag] == SS100THSecondUnitKey && [self convertUnitsPerInchToPixelsPerUnit:[pixelField floatValue]] == 20) {
      //100th second preset
      [presetScaleMatrix selectCellWithTag:SSOverlapPreset100THSecondKey];
   } else if([[unitPopup selectedItem] tag] == SSSecondUnitKey && [self convertUnitsPerInchToPixelsPerUnit:[pixelField floatValue]] == 20) {
      //second preset
      [presetScaleMatrix selectCellWithTag:SSOverlapPresetSecondKey];
   } else if([[unitPopup selectedItem] tag] == SSMinuteUnitKey && [self convertUnitsPerInchToPixelsPerUnit:[pixelField floatValue]] == 20) {
      //minute preset
      [presetScaleMatrix selectCellWithTag:SSOverlapPresetMinuteKey];
   } else if([[unitPopup selectedItem] tag] == SSHourUnitKey && [self convertUnitsPerInchToPixelsPerUnit:[pixelField floatValue]] == 20) {
      //hour preset
      [presetScaleMatrix selectCellWithTag:SSOverlapPresetHourKey];
   } else if([[unitPopup selectedItem] tag] == SSDayUnitKey && [self convertUnitsPerInchToPixelsPerUnit:[pixelField floatValue]] == 20) {
      //day preset
      [presetScaleMatrix selectCellWithTag:SSOverlapPresetDayKey];
   } else if([[unitPopup selectedItem] tag] == SSDayUnitKey && [self convertUnitsPerInchToPixelsPerUnit:[pixelField floatValue]] == 10) {
      //week preset
      [presetScaleMatrix selectCellWithTag:SSOverlapPresetWeekKey];
   } else if([[unitPopup selectedItem] tag] == SSDayUnitKey && [self convertUnitsPerInchToPixelsPerUnit:[pixelField floatValue]] == 5) {
      //month preset
      [presetScaleMatrix selectCellWithTag:SSOverlapPresetMonthKey];
   } else if([[unitPopup selectedItem] tag] == SSDayUnitKey && [self convertUnitsPerInchToPixelsPerUnit:[pixelField floatValue]] == 1) {
      //year preset
      [presetScaleMatrix selectCellWithTag:SSOverlapPresetYearKey];
   } else {
      //no match deselect
      [presetScaleMatrix deselectAllCells];
   }
}

- (IBAction)presetScaleMatrixSelectionChanged:(id)sender
{
   //set the pop up and field values based on the selection
   switch([[presetScaleMatrix selectedCell] tag]) {
      case SSOverlapPresetMillisecondKey:
         [unitPopup selectItemAtIndex:SSMillisecondUnitKey];
         [pixelField setFloatValue:[self convertPixelsPerUnitToUnitsPerInch:20]];
           break; 
      case SSOverlapPreset100THSecondKey:
         [unitPopup selectItemAtIndex:SS100THSecondUnitKey];
         [pixelField setFloatValue:[self convertPixelsPerUnitToUnitsPerInch:20]];
           break;
      case SSOverlapPresetSecondKey:
         [unitPopup selectItemAtIndex:SSSecondUnitKey];
         [pixelField setFloatValue:[self convertPixelsPerUnitToUnitsPerInch:20]];
           break;
      case SSOverlapPresetMinuteKey:
         [unitPopup selectItemAtIndex:SSMinuteUnitKey];
         [pixelField setFloatValue:[self convertPixelsPerUnitToUnitsPerInch:20]];
         break;
      case SSOverlapPresetHourKey:
         [unitPopup selectItemAtIndex:SSHourUnitKey];
         [pixelField setFloatValue:[self convertPixelsPerUnitToUnitsPerInch:20]];
         break;
      case SSOverlapPresetDayKey:
         [unitPopup selectItemAtIndex:SSDayUnitKey];
         [pixelField setFloatValue:[self convertPixelsPerUnitToUnitsPerInch:20]];
         break;
      case SSOverlapPresetWeekKey:
         [unitPopup selectItemAtIndex:SSDayUnitKey];
         [pixelField setFloatValue:[self convertPixelsPerUnitToUnitsPerInch:10]];
         break;
      case SSOverlapPresetMonthKey:
         [unitPopup selectItemAtIndex:SSDayUnitKey];
         [pixelField setFloatValue:[self convertPixelsPerUnitToUnitsPerInch:5]];
         break;
      case SSOverlapPresetYearKey:
         [unitPopup selectItemAtIndex:SSDayUnitKey];
         [pixelField setFloatValue:[self convertPixelsPerUnitToUnitsPerInch:1]];
         break;
      default:
         break;
   }
}

- (float)convertUnitsPerInchToPixelsPerUnit:(float)unitsPerInch
{
   return ([[[[NSScreen mainScreen] deviceDescription] objectForKey:NSDeviceResolution] sizeValue].width / unitsPerInch);
}

- (float)convertPixelsPerUnitToUnitsPerInch:(float)pixelsPerUnit
{
   return ([[[[NSScreen mainScreen] deviceDescription] objectForKey:NSDeviceResolution] sizeValue].width / pixelsPerUnit);
}


@end
